package com.example.jt_manager_web;

import com.example.jt_common.EasyUIResult;
import com.example.jt_common.SysResult;
import com.example.jt_manager_pojo.Item;
import com.example.jt_manager_pojo.ItemCat;
import com.example.jt_manager_pojo.ItemParam;
import com.example.jt_manager_service.ItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("item")
public class GoodsItemController {

    @Autowired
    ItemService itemService;

    @RequestMapping("cat/list")
    @ResponseBody
    public  List<ItemCat> getGoodsCategory(@RequestParam(name = "id",required = false,defaultValue = "0") long id){
        List<ItemCat> itemCats = itemService.queryGoodsCategory(id);
        return itemCats;
    }

    @RequestMapping("save")
    @ResponseBody
    public SysResult itemSave(Item item,String desc){
        item.setCreated(new Date());
        item.setUpdated(new Date());
        itemService.addGood(item,desc);
        return SysResult.ok();
    }

    @RequestMapping("query")
    @ResponseBody
    public EasyUIResult itemQuery(int page, int rows){
        PageInfo<Item> pageInfo = itemService.queryGoodsByPage(page,rows);
        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setTotal(pageInfo.getTotal());
        easyUIResult.setRows(pageInfo.getList());
        return easyUIResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    public SysResult itemDelete(long[] ids){
        List<Long> list = new ArrayList<>();
        for (long id :
                ids) {
            list.add(id);
        }
        if(itemService.deleteGoods(list)==ids.length){
            return SysResult.ok();
        }
        return null;
    }

    @RequestMapping("query/item/desc/{id}")
    @ResponseBody
    public SysResult queryDesc(@PathVariable("id")long id){
        return SysResult.ok(itemService.queryItemDescById(id));
    }

    @RequestMapping("instock")
    @ResponseBody
    public SysResult itemInstock(long[] ids){
        List<Long> list = new ArrayList<>();
        for (long id :
                ids) {
            list.add(id);
        }
        if(itemService.itemInstock(list)==ids.length){
            return SysResult.ok();
        }
        return null;
    }

    @RequestMapping("reshelf")
    @ResponseBody
    public SysResult itemReshelf(long[] ids){
        List<Long> list = new ArrayList<>();
        for (long id :
                ids) {
            list.add(id);
        }
        if(itemService.itemReshelf(list)==ids.length){
            return SysResult.ok();
        }
        return null;
    }


    @RequestMapping("param/list")
    @ResponseBody
    public EasyUIResult paramList(int page,int rows){

        PageInfo<ItemParam> pageInfo = itemService.queryItemParamByPage(page,rows);
        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setTotal(pageInfo.getTotal());
        easyUIResult.setRows(pageInfo.getList());
        return easyUIResult;
    }

    @RequestMapping("param/delete")
    @ResponseBody
    public SysResult paramDelete(long[] ids){
        List<Long> list= new ArrayList<>();
        for (long id :
                ids) {
            list.add(id);
        }
        if(itemService.deleteParamByIds(list)==ids.length){
            return SysResult.ok();
        }
        return SysResult.error();
    }

    @RequestMapping("param/query/itemcatid/{id}")
    @ResponseBody
    public SysResult paramQuery(@PathVariable("id")long id) {
        System.out.println(id);
        if (itemService.isExistParamById(id) == 1) {
            return SysResult.ok();
        }
        return SysResult.error();
    }

    @RequestMapping("param/save/{cid}")
    @ResponseBody
    public SysResult paramSave(@PathVariable("cid")long cid,String paramData){
        ItemParam itemParam = new ItemParam();
        itemParam.setItemCatId(cid);
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        itemParam.setParamData(paramData);
        if(itemService.insertParam(itemParam)==1){
            return SysResult.ok();
        }
        return SysResult.error();
    }


}
